package com.bw.employee.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.po.TbEmployee;
import com.bw.employee.service.TbEmployeeService;
import com.bw.employee.mapper.TbEmployeeMapper;
import com.bw.result.ResponseState;
import com.bw.result.ResultVo;
import com.bw.util.SnowflakeIdWorker;
import com.bw.vo.IdVo;
import com.bw.vo.TbEmployeeSearchVo;
import com.bw.vo.TbEmployeeVo;
import com.github.xiaoymin.knife4j.core.util.CommonUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 花开Mystic
* @description 针对表【tb_employee(员工管理)】的数据库操作Service实现
* @createDate 2023-11-10 21:01:05
*/
@Service
public class TbEmployeeServiceImpl extends ServiceImpl<TbEmployeeMapper, TbEmployee>
    implements TbEmployeeService{

    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0,0);

    @Autowired
    private TbEmployeeMapper tbEmployeeMapper;

    private static final String upperCaseStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final String lowerCaseStr = "abcdefghijklmnopqrstuvwxyz";

    @Override
    public ResultVo add(TbEmployeeVo tbEmployeeVo) {
        tbEmployeeVo.setId(String.valueOf(idWorker.nextId()));
        TbEmployee tbEmployee = new TbEmployee();
        BeanUtils.copyProperties(tbEmployeeVo,tbEmployee);
        // 自动生成员工工号
        String jobCart = RandomUtil.randomNumbers(6);
        tbEmployee.setJobCart(jobCart);
        // 随机生成员工密码
        String password = RandomUtil.randomString(upperCaseStr+lowerCaseStr,4) + RandomUtil.randomNumbers(4);
        tbEmployee.setPassword(password);
        boolean save = save(tbEmployee);
        return save?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo updateByVo(TbEmployeeVo tbEmployeeVo) {
        QueryWrapper<TbEmployee> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbEmployee::getId,tbEmployeeVo.getId());
        TbEmployee employee = tbEmployeeMapper.selectOne(wrapper);
        if(employee == null){
            return ResultVo.FAILED(ResponseState.NOT_EXIST);
        }
        BeanUtils.copyProperties(tbEmployeeVo,employee);
        boolean update = updateById(employee);
        return update?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo delete(IdVo idVo) {
        boolean remove = removeById(idVo.getId());
        return remove?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo listBySearchVo(TbEmployeeSearchVo tbEmployeeSearchVo) {
        Page<TbEmployee> page = new Page<>(tbEmployeeSearchVo.getPageNum(), tbEmployeeSearchVo.getPageSize());
        Page<TbEmployeeVo> tbEmployeeVoPage = tbEmployeeMapper.listBySearchVo(page,tbEmployeeSearchVo);
        return ResultVo.SUCCESS(tbEmployeeVoPage);
    }

}




